package com.itsu.vtalk.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @author suben
 * @createtime 2020/6/22 19:17
 **/
public class URLEncodeDecodeUtil {

    public static String encode(String source) throws UnsupportedEncodingException {
        return URLEncoder.encode(source, "UTF-8");
    }

    public static String decode(String source) throws UnsupportedEncodingException {
        return URLDecoder.decode(source, "UTF-8");
    }

    public static Map<String, String> encodeMap(Map<String, String> map, boolean both) throws UnsupportedEncodingException {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String encodeKey = entry.getKey();
            if (both) {
                encodeKey = encode(entry.getKey());
            }
            String encodeValue = encode(entry.getValue());
            map.put(encodeKey, encodeValue);

        }

        return map;
    }
}
